/*

Copyright (c) 2008, Jonathan Wayne Parrott, Denzel Morris.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

#include "PhParticleSystem.h"

using namespace phoenix;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
/*
	Create
	The particle system behaves much like any
	other scene node, save for the fact that
	it also manages children.
*/
boost::shared_ptr<PhParticleSystem> PhParticleSystem::create(PhSceneManager* s)
{
    boost::shared_ptr<PhParticleSystem> newsys(new PhParticleSystem(s));
    s->addNode( newsys->grab<PhSceneNode>() );
    return newsys->grab<PhParticleSystem>();
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
/*
	Drop
	Just deletes all particles, effectors and emitters.
*/
void PhParticleSystem::drop()
{
    dropEmitters();
    dropParticles();
    dropEffectors();
    PhSceneNode::drop();
}

//////////////////////////////////////////////////////
// Add stuff functions
//////////////////////////////////////////////////////

void PhParticleSystem::addParticle( boost::shared_ptr<PhParticle> p )
{
	particles.addResource( p->grab<PhResource>() );
}

void PhParticleSystem::addEmitter( boost::shared_ptr<PhEmitter> e)
{
	emitters.addResource( e->grab<PhResource>() );
}

void PhParticleSystem::addEffector( boost::shared_ptr<PhEffector> f)
{
	effectors.addResource( f->grab<PhResource>() );
}


//////////////////////////////////////////////////////
// Remove stuff
//////////////////////////////////////////////////////

void PhParticleSystem::removeParticle( boost::shared_ptr<PhParticle> p )
{
	p->drop();
}

void PhParticleSystem::removeEmitter( boost::shared_ptr<PhEmitter> e )
{
	e->drop();
}

void PhParticleSystem::removeEffector( boost::shared_ptr<PhEffector> f )
{
	f->drop();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
/*
	Pre render call we go through every effector
	and call preStep().
	We'll also register to render just like
	every other scene node
*/
void PhParticleSystem::onPreRender()
{

    for (unsigned int i = 0; i < effectors.getResourceCount(); ++i)
    {
        (effectors[i])->grab<PhEffector>()->preStep();
        particles.pruneList();
    }
    effectors.pruneList();

    smanager->registerForRendering(this->grab<PhSceneNode>());
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
/*
	Render call time. All we need to do is draw
	every particle.
*/
void PhParticleSystem::onRender()
{
    for (unsigned int i = 0; i < particles.getResourceCount(); ++i)
    {
        (particles[i])->grab<PhParticle>()->draw();
    }
    particles.pruneList();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
/*
	Post render. All emitters are called,
	Then all effectors.
*/
void PhParticleSystem::onPostRender()
{
    for (unsigned int i = 0; i < emitters.getResourceCount(); ++i)
    {
        (emitters[i])->grab<PhEmitter>()->step();
    }
    emitters.pruneList();
    for (unsigned int i = 0; i < effectors.getResourceCount(); ++i)
    {
        (effectors[i])->grab<PhEffector>()->step();
        particles.pruneList();
    }
    effectors.pruneList();
}
